package org.example.reggie.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * 公共字段自动填充，也是个拦截器，实现了MetaObjectHandler接口
 */
@Component
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Autowired
    private HttpServletRequest httpServletRequest;
    @Override
    public void insertFill(MetaObject metaObject) {

        Long employeeId = (Long) httpServletRequest.getSession().getAttribute("employee");

        metaObject.setValue("createTime", LocalDateTime.now());
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("createUser",BaseContext.getThreadLocal());
        metaObject.setValue("updateUser",BaseContext.getThreadLocal());

    }

    @Override
    public void updateFill(MetaObject metaObject) {
        Long employeeId = (Long) httpServletRequest.getSession().getAttribute("employee");

        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("updateUser",BaseContext.getThreadLocal());

    }
}


/**
 * chatgpt提供的方法
 */
//
//import lombok.extern.slf4j.Slf4j;
//import org.aspectj.lang.ProceedingJoinPoint;
//import org.aspectj.lang.annotation.Around;
//import org.aspectj.lang.annotation.Aspect;
//import org.aspectj.lang.annotation.Pointcut;
//import org.example.reggie.pojo.Employee;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//
//import javax.servlet.http.HttpServletRequest;
//import java.time.LocalDateTime;
//
//@Component
//@Aspect
//@Slf4j
//public class EntityAspect {
//
//    @Autowired
//    private HttpServletRequest request;
//
//    @Pointcut(value = "execution(* org.example.reggie.mapper.EmployeeMapper.*(..))")
//    public void entityAspect() {
//    }
//
//    @Around("entityAspect()")
//    public Object entityAspectHandler(ProceedingJoinPoint pjp) throws Throwable {
//        Long employeeid = (Long) request.getSession().getAttribute("employee");
//        Object[] args = pjp.getArgs();
//        if (args != null && args.length > 0) {
//            for (Object arg : args) {
//                if (arg instanceof Employee) {
//                    Employee entity = (Employee) arg;
//                    if (entity.getCreateTime() == null) {
//                        entity.setCreateTime(LocalDateTime.now());
//                    }
//                    if (entity.getCreateUser() == null) {
//                        entity.setCreateUser(employeeid);
//                    }
//                    if (entity.getUpdateTime() == null) {
//                        entity.setUpdateTime(LocalDateTime.now());
//                    }
//                    if (entity.getUpdateUser() == null) {
//                        entity.setUpdateUser(employeeid);
//                    }
//                }
//            }
//        }
//        return pjp.proceed();
//    }
//
//}
